package com.concurrent.sample.common.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Slf4j
@Configuration
public class RedissonConfig {

  @Bean
  public RedissonClient redissonClient(ObjectMapper objectMapper, RedisProperties redisProperties) {
    Config config = new Config();
    config.setCodec(new JsonJacksonCodec(objectMapper));

    SingleServerConfig singleServerConfig = config.useSingleServer();
    log.info("redis host: {}", redisProperties.getUrl());
    singleServerConfig.setAddress(redisProperties.getUrl()).setConnectionPoolSize(32);
    if (StringUtils.hasLength(redisProperties.getPwd())) {
      singleServerConfig.setPassword(redisProperties.getPwd());
    }
    return Redisson.create(config);
  }

}
